﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using CSCSN.WebApplication.Models;
using CSCSN.ORM;
using CSCSN.EntitiyModels;
using Microsoft.AspNetCore.Identity;
using CSCSN.ViewModels.TakeAndTakeType;
using CSCSN.ViewModels.RoleAndUser;
using Microsoft.EntityFrameworkCore;
using CSCSN.EntitiyModels.ApplicationTaskAbout;

namespace CSCSN.WebApplication.Controllers
{

    public class HomeController : Controller
    {

        #region 数据上下文
        private readonly CsDbContext db;

        public HomeController(CsDbContext logger)
        {
            db = logger;
        }
        #endregion


        #region 首页列表


        public IActionResult Index()
         {

            var item = db.ApplicationTasks.Where(s => s.ApplicationTaskTypes.Name == ("义工时")).Include(s=>s.TaskSites).Include(s=>s.ApplicationRole).OrderByDescending(s => s.Score).Take(4).ToList();
            List<TaskViewModel> listVolunteer = new List<TaskViewModel>();
            foreach (var dd in item)
            {
                TaskViewModel task = TaskToViewModel(dd);
                listVolunteer.Add(task);
            }
            var items = db.ApplicationTasks.Where(s => s.ApplicationTaskTypes.Name == ("诚信分")).Include(s => s.TaskSites).Include(s => s.ApplicationRole).OrderByDescending(s => s.Score).Take(4).ToList();
            List<TaskViewModel> listIntegrity = new List<TaskViewModel>();
            foreach (var dd in items)
            {
                TaskViewModel task = TaskToViewModel(dd);
                listIntegrity.Add(task);
            }
            var itemss = db.ApplicationTasks.Where(s => s.ApplicationTaskTypes.Name == ("活动分")).Include(s => s.TaskSites).Include(s => s.ApplicationRole).OrderByDescending(s => s.Score).Take(4).ToList();
            List<TaskViewModel> listActivitys = new List<TaskViewModel>();
            foreach (var dd in itemss)
            {
                TaskViewModel task = TaskToViewModel(dd);
                listActivitys.Add(task);
            }
            ViewBag.Name = listVolunteer;
            ViewBag.Names = listIntegrity;
            ViewBag.Namess = listActivitys;
            var userIdentity = User.Identity;
            if (userIdentity.Name != null)//判断登陆状态
            {
                ViewBag.userName = userIdentity.Name;
                var user = db.ApplicationUsers.Where(a => a.UserName == User.Identity.Name).FirstOrDefault();
                var userTask = db.ApplicationUserWithApplicationTasks.Where(s => s.ApplicationUsers == user && DateTime.Equals(s.ApplicationTasks.BeginTime.Date, DateTime.Now.Date)).Include(s=>s.ApplicationTasks).Include(s=>s.ApplicationTasks.TaskSites).Include(s=>s.ApplicationTasks.ApplicationRole).Select(s=>s.ApplicationTasks).ToList();//查询用户接取的任务为今天的并排序
                List<ApplicationTask> list = new List<ApplicationTask>();
                TaskViewModel task = new TaskViewModel();
                List<TaskViewModel> listVM = new List<TaskViewModel>();
                foreach (var ss in userTask)
                {
                    
                    if (DateTime.Compare(ss.BeginTime, DateTime.Now) >= 0)//任务开始时间与当前时间进行比较
                    {
                        list.Add(ss);
                    }
                }
                
                foreach (var ss in list)
                {
                    var qq = TaskToViewModel(ss);
                    listVM.Add(qq);
                }

                listVM = listVM.OrderBy(s => s.BeginTime).ToList();//按任务时间大小排序。
                ViewData["CountdownTask"] = listVM;
                if (listVM.Count > 0)
                {
                    ViewBag.Time = listVM[0].BeginTime.ToString("yyyy/MM/dd HH:mm");
                }

                ViewBag.List = list;
            }
            return View();

        }
        #endregion


        #region 任务详情
        public IActionResult Details(Guid id)
        {
            var a = db.ApplicationTasks.Include(s=>s.TaskSites).Include(s=>s.ApplicationRole).Where(s=>s.Id==id).FirstOrDefault();
            TaskViewModel taskViewModel = new TaskViewModel();
            taskViewModel = TaskToViewModel(a);
            return View(taskViewModel);
        }
        #endregion


        #region 数据映射
        /// <summary>
        /// 任务表model向页面model转换
        /// </summary>
        private TaskViewModel TaskToViewModel(ApplicationTask model)
        {
            TaskViewModel item = new TaskViewModel();
            item.id = model.Id;
            item.Name = model.Name;
            item.TaskSite = model.TaskSites.Name;
            item.Score = model.Score;
            item.BeginTime = model.BeginTime;
            item.Duration = model.Duration;
            item.Issuer = model.ApplicationRole.Name;

            return item;
        }
        #endregion

        public IActionResult LKmjun()
        {
            return View();
        }
        public IActionResult Jianj()
        {
            return View();
        }
    }
}
